﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Main.Common;
using Main.UI;
using Main.UI.Components;

namespace Main.Engine
{
    partial class ComponentManager : IComponentManager
    { 
        static ComponentManager()
        {
            Instance = new ComponentManager();
        }

        internal static IComponentManager Instance { get; private set; }
        private Dictionary<Type, IComponent> COMPONENTS = new Dictionary<Type, IComponent>();
        private Type COMPONENT_TYPE = typeof(IComponent);

        private ComponentManager()
        {
            AppDomain.CurrentDomain.FindAll<IComponent>(AssignComponent);
        }

        private void AssignComponent(Type t, IComponent obj)
        {
            IComponent o = (IComponent)obj;
            foreach (Type iFace in t.GetInterfaces().Where(w => COMPONENT_TYPE.IsAssignableFrom(w)))
            {
                if (iFace == COMPONENT_TYPE) continue;
                if (COMPONENTS.ContainsKey(iFace))
                    continue;
                COMPONENTS.Add(iFace, o);
            }

        }
    }
}
